package com.ewanghuiju.app.presenter.pintuan;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.PintuanSureOrderContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.request.PtistarGroupBean;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.bean.response.RuleBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.exception.ApiException;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 确认订单
 * @time 2020/2/16
 */
public class PintuanSureOrderPresenter extends RxPresenter<PintuanSureOrderContract.View> implements PintuanSureOrderContract.Presenter {

    private DataManager mDataManager;
    private int currentType = Constants.TYPE_ADDRESS_SELECTED;

    @Inject
    public PintuanSureOrderPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    /**
     * 开团支付
     *
     * @param ptistarGroupBean
     */
    @Override
    public void nowPay(PtistarGroupBean ptistarGroupBean) {
        addSubscribe(mDataManager.ptistarGroup(ptistarGroupBean.getGoods_id(), ptistarGroupBean.getAddress_id(),
                ptistarGroupBean.getSpecs_id(), ptistarGroupBean.getActual_payment(), ptistarGroupBean.getPay_password(), ptistarGroupBean.getIs_goods(),ptistarGroupBean.getOrder_remark())
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.showPay(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null) {
                            if (e instanceof ApiException) {
                                mView.showPayError(((ApiException) e).getCode(), e.getMessage());
                            }
                        }
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                })
        );
    }

    /**
     * 参团支付
     *
     * @param ptistarGroupBean
     */
    @Override
    public void ptijoinGroup(PtistarGroupBean ptistarGroupBean) {
//        Log.e("pintuan",new Gson().toJson(ptistarGroupBean));
        addSubscribe(mDataManager.ptijoinGroup(ptistarGroupBean.getGroup_id(), ptistarGroupBean.getAddress_id(),
                ptistarGroupBean.getSpecs_id(), ptistarGroupBean.getPay_password(), ptistarGroupBean.getOrder_remark())
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.showPay(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null) {
                            if (e instanceof ApiException) {
                                mView.showPayError(((ApiException) e).getCode(), e.getMessage());
                            }
                        }
                    }
                })
        );
    }

    @Override
    public void attachView(PintuanSureOrderContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == currentType;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        AddressListResponBean addressListResponBean = (AddressListResponBean) s.getData();
                        if (mView != null) {
                            mView.showSelectedAddress(addressListResponBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    /**
     * 判断是否设置支付密码
     */
    @Override
    public void getIsbindPwd() {
        //刷新获取数据
        UserInfoResponBean userInfoResponBean = mDataManager.getUserInfoResponBean();
        if (userInfoResponBean != null && userInfoResponBean.getIs_password() == 1) {
            if (mView != null) {
                mView.isbindPwd();
            }
        } else {
            if (mView != null) {
                mView.nobindPwd();
            }
        }
    }

    @Override
    public void getPtiguize(String type) {
        //获取规则
        addSubscribe(mDataManager.getPtiguize(type)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<RuleBean>>(mView) {
                    @Override
                    public void onNext(Optional<RuleBean> optional) {
                        if (mView != null)
                            mView.showPtiguize(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
}
